﻿package mortal.game.view.task.view.render
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.daily.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class TaskTrackDoing extends TaskTrackBase
    {
        private var _btnQuick:GButton;
        private var _btnZSFinish:GLoadingButton;
        private var _light:ScaleBitmap;
        private var _guideTimerId:int = -1;
        private var _lastGuideStep:int;
        private var _flyBoot:FlyBoot;
        private var _isGuiding:Boolean = false;
        private var _isGuideFlyBoot:Boolean;

        public function TaskTrackDoing()
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._btnQuick != null)
            {
                this._btnQuick.dispose(param1);
                this._btnQuick = null;
            }
            if (this._btnZSFinish != null)
            {
                this._btnZSFinish.dispose(param1);
                this._btnZSFinish = null;
            }
            this.removeGuide();
            this.removeTitleFlyBoot();
            this._lastGuideStep = -1;
            if (this._guideTimerId > 0)
            {
                clearTimeout(this._guideTimerId);
                this._guideTimerId = -1;
            }
            return;
        }// end function

        override public function set copyInfo(param1:TDailyViewConfig) : void
        {
            super.copyInfo = param1;
            _taskInfo = null;
            DisplayUtil.delNotUse(_txts, 0, true);
            if (param1.times == null)
            {
                _txtTitle.htmlText = "<font color=\'#FAD200\'>【" + param1.output + "】</font><u><a href=\'event:0\'><font color=\'#00ff00\'>" + param1.name + "</font><font color=\'#FAD200\'>(0/1)</font></a></u>";
                this.removeTitleFlyBoot();
            }
            else if (param1.operType == -1)
            {
                _txtTitle.htmlText = "<font color=\'#FAD200\'>【" + param1.output + "】</font><u><a href=\'event:" + param1.times + "\'><font color=\'#00ff00\'>" + param1.name + "</font></a></u>";
                if (param1.bossType == 1)
                {
                    this.addFlyBoot();
                    this._flyBoot.gLinkTextData = null;
                    this._flyBoot.x = _txtTitle.textWidth + 4;
                }
                else
                {
                    this.removeTitleFlyBoot();
                }
            }
            else
            {
                _txtTitle.htmlText = "<font color=\'#FAD200\'>【" + param1.output + "】</font><u><a href=\'event:0\'><font color=\'#00ff00\'>" + param1.name + "</font><font color=\'#FAD200\'>(" + int(Cache.instance.daily.data.times[param1.type]) + "/" + param1.times + ")</font></a></u>";
                this.removeTitleFlyBoot();
            }
            return;
        }// end function

        private function removeTitleFlyBoot() : void
        {
            if (this._flyBoot != null)
            {
                DisplayUtil.removeMe(this._flyBoot);
                this._flyBoot.dispose(true);
                this._flyBoot = null;
            }
            return;
        }// end function

        public function addFlyBoot() : void
        {
            if (this._flyBoot == null)
            {
                this._flyBoot = UICompomentPool.getUICompoment(FlyBoot) as FlyBoot;
                this._flyBoot.configEventListener(MouseEvent.CLICK, this.clickFlyBootHandler);
            }
            this._flyBoot.mouseEnabled = true;
            this._flyBoot.buttonMode = true;
            this.addChild(this._flyBoot);
            return;
        }// end function

        private function clickFlyBootHandler(event:MouseEvent) : void
        {
            if (_dailyInfo == null || _dailyInfo.bossType < 0)
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskClickBranchGuide, [true, _dailyInfo.times]));
            return;
        }// end function

        override public function linkImpl(param1:String) : void
        {
            var _loc_2:Array = null;
            if (_dailyInfo != null)
            {
                if (_dailyInfo.operType == -1)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskClickBranchGuide, [false, param1]));
                }
                else
                {
                    DailyUtil.oper(_dailyInfo);
                }
            }
            else if (_taskInfo != null)
            {
                _loc_2 = Cache.instance.task.getLinkTextData(_taskInfo.stask.code);
                if (_loc_2 == null || _loc_2[0] == null || _loc_2[0][0] == null)
                {
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTraceTargetEvent, _loc_2[0][0]));
            }
            return;
        }// end function

        override public function set taskInfo(param1:TaskInfo) : void
        {
            var _loc_6:GLinkTextData = null;
            var _loc_14:Array = null;
            var _loc_15:int = 0;
            var _loc_16:GLinkText = null;
            var _loc_17:ColorInfo = null;
            var _loc_18:Array = null;
            var _loc_19:int = 0;
            _taskInfo = param1;
            _dailyInfo = null;
            var _loc_2:* = _taskInfo.curStep;
            var _loc_3:* = Cache.instance.task.getLinkTextData(_taskInfo.stask.code);
            var _loc_4:int = 15;
            var _loc_5:* = !_taskInfo.isCanget() || _taskInfo.stask.group == ETaskGroup._ETaskGroupMain || _taskInfo.stask.group == ETaskGroup._ETaskGroupBranch;
            if (_loc_5)
            {
                if (_loc_3 != null)
                {
                    _loc_14 = _loc_3[_loc_2];
                    _loc_15 = 0;
                    while (_loc_14 != null && _loc_15 < _loc_14.length)
                    {
                        
                        _loc_16 = getText(_loc_15);
                        _loc_6 = _loc_14[_loc_15] as GLinkTextData;
                        _loc_16.y = _loc_4;
                        _loc_16.setLinkText(_loc_6, getExtendHtmlText());
                        _loc_4 = _loc_4 + (_loc_16.height - 2);
                        _loc_15++;
                    }
                }
                DisplayUtil.delNotUse(_txts, _loc_15, true);
            }
            else
            {
                DisplayUtil.delNotUse(_txts, 0, true);
            }
            var _loc_7:* = _taskInfo.stask.group;
            var _loc_8:* = TaskRule.getGroupFullName(_loc_7);
            if (_loc_7 == ETaskGroup._ETaskGroupMain || _loc_7 == ETaskGroup._ETaskGroupBranch)
            {
                _loc_8 = _taskInfo.stask.name;
            }
            if (_taskInfo.playerTask != null && _taskInfo.playerTask.extend > 0)
            {
                _loc_17 = ColorConfig.instance.getItemColor(_taskInfo.playerTask.extend);
                if (_loc_17 != null)
                {
                    _loc_8 = "<font color=\'" + _loc_17.color + "\'>" + _taskInfo.stask.name + "</font>";
                }
            }
            var _loc_9:* = Cache.instance.task.getGroupFinishCount(_taskInfo.stask.group);
            var _loc_10:* = TaskRule.getGroupTotalTimes(_taskInfo.stask.group);
            if (!_loc_5)
            {
                _loc_8 = _taskInfo.getTypeName() + "<u><a href=\'event:0\'>" + _loc_8;
                if (_taskInfo.isCanNotEnd() && _loc_6 != null && Cache.instance.role.roleInfo.level < _loc_6.needLevel)
                {
                    _txtTitle.htmlText = _loc_8 + "<font color=\'#ff0000\'>(" + Language.getString(21186) + ")</font></a></u>";
                }
                else if (_loc_10 != 1)
                {
                    _txtTitle.htmlText = _loc_8 + "<font color=\'#FAD200\'>(" + _loc_9 + "/" + _loc_10 + ")</font></a></u>";
                }
                else
                {
                    _txtTitle.htmlText = _loc_8 + _taskInfo.getStatusName(true) + "</a></u>";
                }
                _loc_18 = Cache.instance.task.getLinkTextData(_taskInfo.stask.code);
                if (_loc_18 != null && _loc_18[0] != null && _loc_18[0][0] != null)
                {
                    _loc_6 = _loc_18[0][0] as GLinkTextData;
                    if (_loc_6.needBoot)
                    {
                        this.addFlyBoot();
                        this._flyBoot.gLinkTextData = _loc_6;
                        this._flyBoot.x = _txtTitle.textWidth + 4;
                    }
                    else
                    {
                        this.removeTitleFlyBoot();
                    }
                }
            }
            else
            {
                this.removeTitleFlyBoot();
                if (_taskInfo.isCanNotEnd() && _loc_6 != null && Cache.instance.role.roleInfo.level < _loc_6.needLevel)
                {
                    _txtTitle.htmlText = _taskInfo.getTypeName() + _loc_8 + "<font color=\'#ff0000\'>(" + Language.getString(21186) + ")</font></a></u>";
                }
                else if (_loc_10 != 1)
                {
                    _txtTitle.htmlText = _taskInfo.getTypeName() + _loc_8 + "<font color=\'#FAD200\'>(" + _loc_9 + "/" + _loc_10 + ")</font>";
                }
                else
                {
                    _txtTitle.htmlText = _taskInfo.getTypeName() + _loc_8 + _taskInfo.getStatusName(true);
                }
            }
            var _loc_11:String = "";
            var _loc_12:Boolean = false;
            var _loc_13:* = _taskInfo.stask.canQuickComplete;
            if (_taskInfo.isFail())
            {
                if (_taskInfo.stask.group == ETaskGroup._ETaskGroupEscort)
                {
                    _loc_11 = "";
                }
                else if (TaskUtil.isFailTaskNeedComplete(_taskInfo))
                {
                    _loc_11 = Language.getString(20183);
                }
                else
                {
                    _loc_11 = Language.getString(20164);
                }
            }
            else if (_loc_13 != ETaskQuick._ETaskQuickNo)
            {
                if (_taskInfo.isComplete())
                {
                    if ((_loc_13 & ETaskQuick._ETaskQuickFinish) > 0)
                    {
                        _loc_11 = Language.getString(20183);
                    }
                }
                else if (_loc_13 == (ETaskQuick._ETaskQuickFinish | ETaskQuick._ETaskQuickComplete))
                {
                    _loc_11 = Language.getString(20166);
                    _loc_12 = true;
                }
                else if (_loc_13 == ETaskQuick._ETaskQuickComplete)
                {
                    _loc_11 = Language.getString(20166);
                }
            }
            if (_taskInfo.isCanget())
            {
                _loc_11 = "";
                _loc_12 = false;
            }
            if (_loc_11 != "" && !_loc_12)
            {
                DisplayUtil.removeMe(this._btnZSFinish);
                if (this._btnQuick == null)
                {
                    _loc_19 = 22;
                    if (_loc_16 != null)
                    {
                        _loc_19 = _loc_16.y + _loc_16.height + 2;
                    }
                    this._btnQuick = UIFactory.gButton("", 12, _loc_19, 60, 22, _bodySprite);
                    this._btnQuick.configEventListener(MouseEvent.CLICK, this.clickFinishHandler);
                }
                if (this._btnQuick.parent == null)
                {
                    _bodySprite.addChild(this._btnQuick);
                }
                this._btnQuick.label = _loc_11;
                this._btnQuick.mouseEnabled = true;
                if (_loc_11.length > 4)
                {
                    this._btnQuick.width = 85;
                }
                else
                {
                    this._btnQuick.width = 60;
                }
            }
            else
            {
                DisplayUtil.removeMe(this._btnQuick);
                if (_loc_12)
                {
                    if (this._btnZSFinish == null)
                    {
                        _loc_19 = 22;
                        if (_loc_16 != null)
                        {
                            _loc_19 = _loc_16.y + _loc_16.height + 2;
                        }
                        this._btnZSFinish = UIFactory.gLoadingButton(ImagesConst.taskZSFinishBtn_upSkin, 12, _loc_19, 64, 20, this);
                        this._btnZSFinish.configEventListener(MouseEvent.CLICK, this.clickQuickFinishHandler);
                    }
                    if (this._btnZSFinish.parent == null)
                    {
                        this.addChild(this._btnZSFinish);
                    }
                }
                else
                {
                    DisplayUtil.removeMe(this._btnZSFinish);
                }
            }
            return;
        }// end function

        private function clickQuickFinishHandler(param1 = null) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickCompleteFinish, _taskInfo));
            return;
        }// end function

        private function linkClickedHandler(param1 = null) : void
        {
            if (!this._isGuiding)
            {
                return;
            }
            this.removeGuide();
            return;
        }// end function

        override public function get isGuiding() : Boolean
        {
            if (_taskInfo == null || _dailyInfo != null)
            {
                return false;
            }
            return this._isGuiding;
        }// end function

        override public function updateGuide() : void
        {
            var _loc_6:TaskInfo = null;
            var _loc_7:GLinkText = null;
            var _loc_8:DisplayObject = null;
            if (_taskInfo == null)
            {
                return;
            }
            var _loc_1:Boolean = false;
            if (_taskInfo.stask.group == ETaskGroup._ETaskGroupLoop && Cache.instance.role.roleInfo.level < GameConst.GuideLoopTasklv)
            {
                _loc_6 = Cache.instance.task.getTaskByGroupInTaskDoing(ETaskGroup._ETaskGroupMain)[0];
                if (_loc_6 != null && _loc_6.isCanNotEnd())
                {
                    _loc_1 = true;
                }
            }
            if (!_loc_1 && _taskInfo.isCanget())
            {
                return;
            }
            this._isGuideFlyBoot = _loc_1 || _taskInfo.stask.guide == TaskGuideType.Guide_FlyBoot;
            if (_taskInfo.isCanget())
            {
                this.removeGuide();
                this.guideTitleArrow();
                return;
            }
            if (_txts == null || _txts.length == 0)
            {
                this.removeGuide();
                return;
            }
            var _loc_2:* = (_txts[0] as GLinkText).myData;
            if (!_loc_1 && (!this._isGuideFlyBoot || !TaskUtil.isTaskCurNeedGuide(_taskInfo, _loc_2)))
            {
                this.removeGuide();
                return;
            }
            if (_taskInfo.curStep == this._lastGuideStep)
            {
                return;
            }
            this._lastGuideStep = _taskInfo.curStep;
            if (this._guideTimerId > 0)
            {
                clearTimeout(this._guideTimerId);
                this._guideTimerId = -1;
            }
            this._isGuiding = true;
            this._guideTimerId = setTimeout(this.guideTimeOut, 4500000);
            var _loc_3:* = _txts[(_txts.length - 1)];
            var _loc_4:* = _txts[0];
            var _loc_5:int = 0;
            while (_loc_5 < _txts.length)
            {
                
                _loc_7 = _txts[_loc_5] as GLinkText;
                _loc_2 = _loc_7.myData;
                if (_loc_2.totalNum > 0 && _loc_2.curNum == _loc_2.totalNum)
                {
                    _loc_7.clickHandler = null;
                }
                else
                {
                    if (!this._isGuideFlyBoot || _loc_1)
                    {
                        if (this._light != null)
                        {
                            DisplayUtil.removeMe(this._light);
                            this._light.dispose(true);
                        }
                        this._light = EffectManager.getTaskLightMask(this.width + 12, super.height - 8);
                        this._light.x = _loc_4.x - 2;
                        this._light.y = _loc_4.y - 2;
                    }
                    else
                    {
                        _loc_8 = _loc_3.flyBoot;
                        if (_loc_8 != null)
                        {
                            if (this._light != null)
                            {
                                DisplayUtil.removeMe(this._light);
                                this._light.dispose(true);
                            }
                            this._light = EffectManager.getTaskLightMask(30, 25);
                            this._light.x = _loc_3.x + _loc_8.x - 2;
                            this._light.y = _loc_3.y + _loc_8.y;
                        }
                    }
                    if (this._light != null)
                    {
                        _bodySprite.addChild(this._light);
                        EffectManager.glowFilterReg(this._light, [FilterConst.guideTaskFilter], 0.2, 7, 0);
                        _loc_7.clickHandler = this.linkClickedHandler;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_TaskTrackArrow, [true, this._light.x + this._light.width / 2, this._light.y + 7, _loc_2, this._isGuideFlyBoot, _loc_1]));
                        break;
                    }
                }
                _loc_5++;
            }
            return;
        }// end function

        override public function get width() : Number
        {
            var _loc_3:GLinkText = null;
            var _loc_4:int = 0;
            if (_txts == null || _txts.length == 0)
            {
                if (this._flyBoot != null)
                {
                    return this._flyBoot.x + this._flyBoot.width;
                }
                if (_txtTitle != null && _txtTitle.parent != null)
                {
                    return _txtTitle.textWidth;
                }
                return 180;
            }
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            while (_loc_2 < _txts.length)
            {
                
                _loc_3 = _txts[_loc_2];
                _loc_4 = _loc_3.width;
                if (_loc_4 > _loc_1)
                {
                    _loc_1 = _loc_4;
                }
                _loc_2++;
            }
            return _loc_1;
        }// end function

        private function guideTitleArrow() : void
        {
            if (this._light != null)
            {
                DisplayUtil.removeMe(this._light);
                this._light.dispose(true);
            }
            this._light = EffectManager.getTaskLightMask(this.width + 12, this.height + 8);
            this._light.x = _txtTitle.x;
            this._light.y = _txtTitle.y - 2;
            this._isGuiding = true;
            _bodySprite.addChild(this._light);
            EffectManager.glowFilterReg(this._light, [FilterConst.guideTaskFilter], 0.2, 7, 0);
            _txtTitle.addEventListener(MouseEvent.CLICK, this.linkClickedHandler);
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_TaskTrackArrow, [true, this._light.x + this._light.width / 2, this._light.y + 7, _taskInfo.getCurLinkData(), this._isGuideFlyBoot, true]));
            return;
        }// end function

        private function guideTimeOut() : void
        {
            this._guideTimerId = -1;
            this.removeGuide();
            return;
        }// end function

        override public function removeGuide() : void
        {
            if (_txtTitle != null)
            {
                _txtTitle.removeEventListener(MouseEvent.CLICK, this.linkClickedHandler);
            }
            if (this._light != null)
            {
                DisplayUtil.removeMe(this._light);
                this._light.dispose(true);
                this._light = null;
            }
            this._isGuiding = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_TaskTrackArrow, [false]));
            if (this._guideTimerId > 0)
            {
                clearTimeout(this._guideTimerId);
                this._guideTimerId = -1;
            }
            return;
        }// end function

        private function clickFinishHandler(event:MouseEvent) : void
        {
            if (_taskInfo != null && _taskInfo.stask.group != ETaskGroup._ETaskGroupLoop)
            {
                this._btnQuick.mouseEnabled = false;
            }
            switch(this._btnQuick.label)
            {
                case Language.getString(20166):
                {
                    if (_taskInfo != null && _taskInfo.stask.canQuickComplete == ETaskQuick._ETaskQuickComplete)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickComplete, _taskInfo));
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickCompleteFinish, _taskInfo));
                    }
                    break;
                }
                case Language.getString(20183):
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickFinish, _taskInfo));
                    break;
                }
                case Language.getString(20164):
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskGiveupTask, _taskInfo));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override public function get height() : Number
        {
            if (this._btnQuick != null && this._btnQuick.parent != null)
            {
                return this._btnQuick.y + this._btnQuick.height;
            }
            if (this._btnZSFinish != null && this._btnZSFinish.parent != null)
            {
                return this._btnZSFinish.y + this._btnZSFinish.height;
            }
            return super.height;
        }// end function

    }
}
